package com.leadbank.stormstudy.util;

import com.leadbank.stormstudy.kafka.PaymentInfo;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * Describe: 随机生产订单消息，此服务单独部署
 */
public class PaymentInfoProducer {

    private final static String TOPIC = "paymentInfo";
    public static void main(String[] args) {
        // 设置配置信息
        Properties props = new Properties();
        props.put("bootstrap.servers", "10.1.21.5:9092,10.1.21.6:9092,10.1.21.7:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        // 创建producer
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
        // 发送数据
        for(int i = 0; i < 1000; i++) {
            producer.send(new ProducerRecord<String, String>(TOPIC, PaymentInfoProducer.genPaymentInfo()));
        }
    }

    private static String genPaymentInfo(){
        return new PaymentInfo().random();
    }
}
